package com.fz.controller;

import com.fz.common.Result;
import com.fz.controller.request.UserPageRequest;
import com.fz.pojo.User;
import com.fz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    //查询用户
    @GetMapping("/list")
    public Result list() {
        List<User> users = userService.list();
        return Result.success(users);
    }

    //通过id查询用户
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    //分页
    @GetMapping("/page")
    public Result page(UserPageRequest userPageRequest) {
        return Result.success(userService.page(userPageRequest));
    }


    // 添加
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user) {//这里要加@@RequestBody 如果不加添加都是null
        userService.addUser(user);
        return Result.success();
    }

    //更新
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        userService.deleteById(id);
        return Result.success();
    }
    //充值金额
    @PostMapping("/account")
    public Result addAccount(@RequestBody User user) {//这里要加@@RequestBody 如果不加添加都是null
        userService.addAccount(user);
        return Result.success();
    }


}
